/**
 * Created by Administrator on 2016/10/26.
 * 说明node.js的运行路径很重要
 */
var http =require("http");
var fs=require("fs");
var url=require("url");
var path=require("path");
var server =http.createServer(function (req,res) {
    var pathname=url.parse(req.url).pathname;
    if (pathname.indexOf(".") == -1) {
        pathname+="/index.html"
    }
    var extname=path.extname(pathname);
    console.log(extname)
   var fileurl="./"+path.normalize("./static/"+pathname);
    console.log(fileurl);
    fs.readFile(fileurl,function(err,data){
        console.log(err)
        if (err) {
          res.writeHead(404,{"Content-Type":"text/html;charset=UTF8"});
          res.end("404,页面没找到")
        }

       getMime(extname, function (mine) {
            res.writeHead(200,{"Content-Type":mine+";charset=UTF8"});
            res.end(data);
        })

    });
});
function getMime(extName,callback) {
    fs.readFile("./mime.json", function (err,data) {
        if (err) {
            throw  Error("未找到");
            return;
        }
        var mimeJSON=JSON.parse(data);
        var mime=mimeJSON[extName]||"text/plain";
        callback(mime);
    })
}
server.listen(8000,"127.0.0.1");
